<template>
  <!-- 添加或修改发票管理对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">


      <el-descriptions class="margin-top " :contentStyle="CS" :label-style="LS" title="" :column="2" size="medium"
                       border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-c-scale-to-original"></i>
            客户名称
          </template>
          {{ invoiceInfo.customerName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            税号
          </template>
          {{ invoiceInfo.taxNo }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            开票金额
          </template>
          {{ invoiceInfo.amount }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            核销金额
          </template>
          {{ invoiceInfo.pinMoney }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            剩余核销
          </template>
          {{ invoiceInfo.pinRemain }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-c-scale-to-original"></i>
            订单编号
          </template>
          {{ invoiceInfo.projectNo }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-bank-card"></i>
            开票明细
          </template>
          <file-upload-show :value="invoiceInfo.files"></file-upload-show>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            开票时间
          </template>
          {{ parseTime(invoiceInfo.invoiceDate, '{y}-{m}-{d} {h}:{i}:{s}') }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-bank-card"></i>
            发票文件
          </template>
          <file-upload-show :value="invoiceInfo.invoiceFile"></file-upload-show>
        </el-descriptions-item>
      </el-descriptions>
      <!--申请明细-->
      <goods-invoice-apply-detail-list-section :applyId="applyId"></goods-invoice-apply-detail-list-section>

      <!--核销记录-->
      <goods-invoice-detail-list-section :invoiceId="applyId" ></goods-invoice-detail-list-section>

      <el-tabs class="detail-tab" type="card" v-model="activeName">
        <el-tab-pane label="核销信息" name="first">
          <el-col :span="12">
            <el-form-item label="核销日期" prop="pinDate">
              <el-date-picker clearable
                              placeholder="请选择开票日期"
                              type="date"
                              v-model="form.pinDate"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核销金额" prop="pinMoney">
              <el-input-number :max="invoiceInfo.pinRemain" :precision="2" placeholder="请输入开票金额"
                               v-model="form.pinMoney"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="pinMoney">
              <el-input placeholder="请输入备注" v-model="form.remark"/>
            </el-form-item>
          </el-col>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"/>
  </el-dialog>
</template>

<script>
import {addAction, deleteAction, getAction, getById, updateAction} from '@/api/manage'
import {updateInvoice} from '@/api/develop/goodsInvoiceBatch'
import FileUploadShow from '../../../../components/FileUpload/FileUploadShow'
import GoodsInvoiceApplyDetailListSection
  from "@/views/develop/goodsInvoiceApplyDetail/GoodsInvoiceApplyDetailListSection.vue";
import GoodsInvoiceDetailListSection from "@/views/develop/goodsInvoiceDetail/GoodsInvoiceDetailListSection.vue";

export default {
  name: "GoodsInvoiceFormDhx",
  dicts: ['company', 'goods_supplier', 'goods_customer', 'fund_callback', 'yes_no', 'invoice_back_status', 'goods_customer'],
  components: {
    GoodsInvoiceDetailListSection,
    GoodsInvoiceApplyDetailListSection,
    FileUploadShow,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      invoiceId: "",
      // 是否显示弹出层
      open: false,
      invoiceInfo:{},
      // 表单参数
      form: {},
      columns: [],
      dataList: [],
      // 表单校验
      rules: {
        customerId: [
          {required: true, message: "客户名称不能为空", trigger: "blur"}
        ],
        no: [
          {required: true, message: "发票编号不能为空", trigger: "blur"}
        ],
        fundCallback: [
          {required: true, message: "是否回款不能为空", trigger: "blur"}
        ],
      },
      total: 0,
      url: {
        getById: '/goodsInvoice/goodsInvoice/',
        add: '/goodsInvoice/goodsInvoice/add',
        edit: '/goodsInvoice/goodsInvoice/edit',
        hexiao: '/goodsInvoiceDetail/goodsInvoiceDetail/hexiao',
      },
      queryParams: {
        id: null,
        no: null,
        amount: null,
        amountReal: null,
        remaining: null,
        customerId: null,
        customerName: null,
        taxNo: null,
        invoiceDate: null,
        invoiceCompany: null,
        fundCallback: null,
        backDate: null,
        pinDate: null,
        drawerId: null,
        drawerName: null,
        saleId: null,
        saleName: null,
        files: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        pinMoney: null,
        pinRemain: null,
        invoiceId: null,
      },
      taxNo: "",
      applyId: "",
    };
  },
  created() {
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit('ok');
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        amount: null,
        amountReal: null,
        remaining: null,
        customerId: null,
        customerName: null,
        taxNo: null,
        invoiceDate: null,
        invoiceCompany: null,
        fundCallback: null,
        backDate: new Date,
        pinDate: new Date,
        drawerId: null,
        drawerName: null,
        saleId: this.$store.getters.userId,
        saleName: this.$store.getters.nickName,
        files: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let that = this;
      this.applyId = row.id;
      that.open = true;
      that.reset();
      const id = row.id || that.ids;
      that.invoiceId = id.toString();
      getById(that.url.getById, id).then(response => {
        that.invoiceInfo = response.data;
        that.form.invoiceId = response.data.id;
        that.form.pinMoney = response.data.pinRemain;
        if (that.form.pinDate != null) {
        } else {
          that.form.pinDate = new Date();
        }
        that.title = "开票核销";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addAction(this.url.hexiao, this.form).then(response => {
            this.$modal.msgSuccess("核销成功");
            this.open = false;
            this.$emit('ok');
          });
        }
      });
    },
  }
};
</script>
